<?php
/**
 * Created by PhpStorm.
 * User: DaveTang
 * Date: 2020/3/3
 * Time: 15:56
 * 用户信息缓存组件
 */

namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Cache\Cache;

class ToolCacheComponent extends Component
{

    public $_cache_config = 'tools';
    public $_page_num = 30;
    public $components = ['CategoryContentCache'];
    public function initialize(array $config)
    {
        parent::initialize($config);
    }

    /**
     * 具体信息
     */
    public function getVal($id)
    {
        $key = 'tool_' . $id;
        $data = Cache::read($key, $this->_cache_config);
        return empty($data) ? $this->createCache($id) : $data;
    }

    /**
     * 创建缓存
     */
    public function createCache($id)
    {
        $key = 'tool_' . $id;
        $controller = $this->_registry->getController();
        $controller->loadModel('Tools');
        $data = $controller->Tools->find()
            ->where(['id' => $id])
            ->first();
        $cache = empty($data) ? null : $data;
        Cache::write($key, $cache, $this->_cache_config);
        return $cache;
    }

    /**
     * 删除列表
     */
    public function deleteList()
    {
        Cache::delete('list', $this->_cache_config);
        return true;
    }

    /**
     * 删除缓存
     */
    public function deleteCache($id)
    {
        $key = 'tool_' . $id;
        Cache::delete($key, $this->_cache_config);
        return true;
    }

    /**
     * 获取列表信息
     */
    public function getList($data)
    {
        if(!empty($data)){
            ksort($data, SORT_NATURAL);
        }
        $key = md5(json_encode($data));
        $controller = $this->_registry->getController();
        $controller->loadModel('Tools');
        $controller->loadModel('CategoryContents');
        $cache = Cache::read('list', $this->_cache_config);
        if(empty($cache[$key])){
            $category_where = [];
            $where = [];
            $category_where['content_type'] = 'tools';
            if(!empty($data['category_id'])){
                $category_where['category_id'] = $data['category_id'];
                $category_content = $controller->CategoryContents->find()
                    ->select(['content_id'])
                    ->where($category_where)
                    ->order(['order_num desc'])
                    ->toArray();
                if(!empty($category_content)){
                    $where['id in'] = array_column($category_content, 'content_id');
                }
            }
            if(isset($data['enabled']) && in_array($data['enabled'], [0,1])){
                $where['enabled'] = $data['enabled'];
            }
            $page = empty($data['page']) ? 1 :$data['page'];
            if(!empty($data['title'])){
                $where['title like'] = '%'.$data['title'].'%';
            }
            $tool = $controller->Tools->find()
                ->select(['id'])
                ->where($where)
                ->limit($this->_page_num)
                ->page($page);
            $cache[$key]['total'] = $tool->count();
            $tool = $tool->toArray();
            $cache[$key]['list'] = empty($tool) ? [] : array_column($tool, 'id');
            Cache::write('list', $cache, $this->_cache_config);
        }
        return $cache;
    }

}